﻿using System;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNViewFormatDTPickerWorker
	{
		
		public static void  formatDTPicker(View view)
		{
			XmlElement root = view.Doc.DocumentElement;

            //CID 0129 //2008.10.02 //Begin
            string chooseFilter = NamespaceUtil.toFullName(view.XslPrefix, "attribute") + "[@name='style']/" + NamespaceUtil.toFullName(view.XslPrefix, "choose");
            //CID 0129 //2008.10.02 //End

            string styleAttrFilter = ".//" + NamespaceUtil.toFullName(view.XslPrefix, "attribute") + "[@name='style']";

			string imgSrcFilter = "//img[@src='res://infopath.exe/calendar.gif']/@src";
			XmlNodeList imgSrcAttrList = DOMUtil.selectNodes(root, imgSrcFilter, 0, view.NsResolver);
			foreach (XmlAttribute imgSrcAttr in imgSrcAttrList)
			{
				imgSrcAttr.Value = view.Manifest.ImageUri.Calendar;
				XmlElement buttonElement = (XmlElement)imgSrcAttr.OwnerElement.ParentNode;

				//CID 0121 //2008.09.26 //Begin
				XmlElement divElement = buttonElement.ParentNode as XmlElement;
				if( divElement != null )
				{
					string style = divElement.GetAttribute( "style" );
                    //CID 1171 //2012.04.24 //if
                    string width = getStyleRuleValue(style, "WIDTH");
                    if (isPreciseWidth(width))
                    {
                        //CID 1171 //2012.04.24 //End
                        //CID 0834 //2011.03.15 //Begin
                        string divStyle = style + ";margin:0px;padding:0px;border:0px;";
                        divElement.SetAttribute("style", divStyle);
                        //CID 0834 //2011.03.15 //End

                        //CID 0834 //2011.03.15 //Begin
                        XmlElement tableElement = view.Doc.CreateElement("table");                        

                        XmlElement spanElement = DOMUtil.selectANode(divElement, "span", view.NsResolver) as XmlElement;
                        //CID 0834 //2011.03.15 //Begin
                        //CID 0886 //2011.04.30 //Begin
                        string spanStyle = null;
                        if (width != null && width.IndexOf("%") > 0)
                        {
                            spanStyle = style + ";margin:0px;";
                        }
                        else
                        {
                            spanStyle = style + ";width:100%;margin:0px;";
                        }
                        spanElement.SetAttribute("spanWidth", width);
                        //CID 0886 //2011.04.30 //End
                        updateStyleAttribute(view, spanElement, spanStyle, styleAttrFilter);
                        spanElement.SetAttribute("class", "xdTextBox");
                        //CID 0873 //2011.04.27 //Begin
                        spanElement.SetAttribute("isDTText", "true");
                        //CID 0873 //2011.04.27 //End
                        //CID 0834 //2011.03.15 //End

                        //CID 0834 //2011.03.15 //Begin
                        string tableStyle = "";
                        if (width != null && width.Length > 0)
                        {
                            tableStyle += "WIDTH:" + width;
                        }
                        tableStyle += ";table-layout:fixed;border:0px;margin:0px;padding:0px;VERTICAL-ALIGN: top;display:inline;";
                        tableElement.SetAttribute("style", tableStyle);
                        tableElement.SetAttribute("cellpadding", "0");
                        tableElement.SetAttribute("cellspacing", "0");

                        XmlElement trElement = view.Doc.CreateElement("tr");
                        XmlElement td1Element = view.Doc.CreateElement("td");
                        //CID 0877 //2011.04.27 //Begin
                        if (width != null && width.IndexOf("%") > 0)
                        {
                            td1Element.SetAttribute("style", "width:" + width + ";");
                        }
                        //CID 0877 //2011.04.27 //End
                        XmlElement td2Element = view.Doc.CreateElement("td");
                        td2Element.SetAttribute("style", "width:22px;");
                        tableElement.AppendChild(trElement);
                        trElement.AppendChild(td1Element);
                        trElement.AppendChild(td2Element);

                        XmlElement boxElement = view.Doc.CreateElement("div");
                        boxElement.AppendChild(spanElement);

                        buttonElement.ParentNode.InsertBefore(tableElement, buttonElement);
                        td1Element.AppendChild(boxElement);
                        td2Element.AppendChild(buttonElement);

                        string buttonStyle = buttonElement.GetAttribute("style");
                        buttonStyle += ";margin:0px 1px;";
                        buttonElement.SetAttribute("style", buttonStyle);
                        //CID 1171 //2012.04.24 //Begin
                        //CID 0877 //2011.04.27 //Begin
                        buttonElement.SetAttribute("onclick", "return InfoJet_OnDatePickerClick(event, this, false);");
                        //CID 0877 //2011.04.27 //End
                        //CID 1171 //2012.04.24 //End
                        //CID 0834 //2011.03.15 //End
                    }
                    else
                    {
                        //CID 1171 //2012.04.24 //Begin
                        view.HasImpreciseWidthDTPicker = true;
                        //CID 1171 //2012.04.24 //End
                        XmlElement spanElement = DOMUtil.selectANode(divElement, "span", view.NsResolver) as XmlElement;
                        if (spanElement != null)
                        {
                            //CID 1171 //2012.04.24 //Begin
                            spanElement.SetAttribute("imprecise", "1");
                            //CID 1171 //2012.04.24 //End
                            string fontSize = getStyleRuleValue(style, "FONT-SIZE");
                            string fontFamily = getStyleRuleValue(style, "FONT-FAMILY");
                            string fontStyle = "";
                            if (fontSize != null && fontSize.Length > 0)
                            {
                                fontStyle = fontStyle + ";FONT-SIZE:" + fontSize;
                            }
                            if (fontFamily != null && fontFamily.Length > 0)
                            {
                                fontStyle = fontStyle + ";FONT-FAMILY:" + fontFamily;
                            }
                            //CID 0276 //2009.05.03 //Begin
                            fontStyle = fontStyle + ";background-color:transparent;";
                            //CID 0276 //2009.05.03 //End
                            string spanStyle = spanElement.GetAttribute("style");
                            if (spanStyle != null && spanStyle.Length > 0)
                            {
                                spanStyle = spanStyle + fontStyle;
                            }
                            else
                            {
                                spanStyle = fontStyle;
                            }
                            spanElement.SetAttribute("style", spanStyle);
                            //CID 0129 //2008.10.02 //Begin
                            XmlElement firstChooseElement = DOMUtil.selectANode(spanElement, chooseFilter, view.NsResolver) as XmlElement;
                            if (firstChooseElement != null)
                            {
                                XmlElement attributeElement = firstChooseElement.ParentNode as XmlElement;
                                XmlText fontStyleText = view.Doc.CreateTextNode(fontStyle);
                                attributeElement.InsertBefore(fontStyleText, firstChooseElement);
                            }
                            //CID 0129 //2008.10.02 //End

                            //CID 1171 //2012.04.24 //Begin
                            XmlElement btnElement = DOMUtil.selectANode(divElement, "button", view.NsResolver) as XmlElement;
                            //CID 0877 //2011.04.27 //Begin
                            btnElement.SetAttribute("onclick", "return InfoJet_OnDatePickerClick(event, this, true);");
                            //CID 0877 //2011.04.27 //End
                            //CID 1171 //2012.04.24 //End
                        }
                    }
                    //CID 1171 //2012.04.24 //End
				}
				//CID 0121 //2008.09.26 //End
			}
		}

        //CID 1171 //2012.04.24 //Begin
        private static bool isPreciseWidth(string width)
        {
            width = width.Trim();
            if (width.EndsWith("%"))
            {
                if (!width.Equals("100%"))
                {
                    return false;
                }
            }
            return true;
        }
        //CID 1171 //2012.04.24 //End

		//CID 0121 //2008.09.26 //Begin
		private static string getStyleRuleValue( string style, string rule )
		{
			rule = rule + ":";
			int begin = style.IndexOf( rule );
			if( begin >= 0 )
			{
				int end = style.IndexOf( ";", begin );
				if( end < 0 )
				{
					end = style.Length;
				}
				begin = begin + rule.Length;
				string ruleValue = style.Substring( begin, end - begin );
				return ruleValue.Trim();
			}
			return null;
		}
		//CID 0121 //2008.09.26 //End

        //CID 0834 //2011.03.15 //Begin
        private static void updateStyleAttribute(View view, XmlElement spanElement, string style, string styleAttrFilter)
        {
            spanElement.SetAttribute("style", style);

            XmlElement attrElement = (XmlElement)DOMUtil.selectANode(spanElement, styleAttrFilter, view.NsResolver);
            if (attrElement != null)
            {
                XmlText styleText = view.Doc.CreateTextNode(style);
                attrElement.InsertBefore(styleText, attrElement.FirstChild);
            }
        }
        //CID 0834 //2011.03.15 //End
	}
}